<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
//种植管理
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$return['setting']=$this->plugin_setting('basic');
$worker=pdo_get('jy_farmer_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
if(!$worker){
	exi('找不到员工信息！','error','jy_newnong/myhome/');
}
$return['worker']=$worker;
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['params']=array('status'=>intval($_RQ['status'])?:1);
	exi($return);
}
/*******op*******/
//待种植
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		if($_RQ['status']==3){
			$params['status >']=2;
		}else{
			$params['status']=$_RQ['status'];
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_plant_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	foreach($list as &$item){
		$item['statustitle']=$this->gdopt_status[$item['status']];
		$item['plant']=pdo_get('jy_farmer_plant',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['plant_id']));
		$item['lclassify_id']=pdo_getcolumn('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['plant']['land_id']),'classify_id');
		$item['land']=pdo_get('jy_farmer_land',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['plant']['land_id']));
		$item['land']['image']=tomedia($item['land']['image']);
		$item['myseed']=pdo_get('jy_farmer_myseed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['myseed_id']));
		$item['seed']=pdo_get('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['myseed']['seed_id']));
		$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>0,'type'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
		if($totype){
				$item['totype0']=1;
		}
		$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>1,'type'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
		if($totype){
				$item['totype1']=1;
		}
		$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>2,'type'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
		if($totype){
				$item['totype2']=1;
		}
		$totype=pdo_getcolumn('jy_farmer_worker_log',array('createtime >'=>$daytime,'pid'=>$item['id'],'totype'=>3,'type'=>1,'uniacid'=>$_SESSION['uniacid']),'id');
		if($totype){
				$item['totype3']=1;
		}
		$shoudaytime=$item['seed']['growtime']*24*3600+$item['createtime'];
		$item['seed']['shouday']=date('Y-m-d',$shoudaytime);
		if($shoudaytime<time() && $item['status']==2){
			$item['isshou']=1;
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='bozhong'){
	if(!$_RQ['id']){
		exi('无法种植，请重新选择','error');
	}
	pdo_update('jy_farmer_plant_log',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('播种已完成');
}
if($_RQ['op']=='success'){
	if(!$_RQ['id']){
		exi('出现错误，请重新选择','error');
	}
	pdo_update('jy_farmer_plant_log',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('收获已完成');
}
if($_RQ['op']=='status4'){
	if($_RQ['id']){
		if(!$_RQ['shoukg']){
			exi('请填写收获重量，单位KG！','error');
		}
		pdo_update('jy_farmer_plant_log',array('status'=>4,'shoukg'=>$_RQ['shoukg']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		exi('操作成功！');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='adopt'){
	if($_RQ['id']){
		$plant_id=pdo_getcolumn('jy_farmer_plant_log',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'plant_id');
		$orderid=pdo_getcolumn('jy_farmer_plant',array('id'=>$plant_id,'uniacid'=>$_SESSION['uniacid']),'order_id');
		$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'浇水一次',
			'totype'=>0,
			'type'=>1,
			'status'=>1,
		);
		$this->model('worker')->uplog($data,$worker['id']);
		exi('浇水成功！','','/worker_plant//status=2');
	}else{
		exi('该果树不存在，请重新选择','error');
	}
}
if($_RQ['op']=='weed'){
	$plant_id=pdo_getcolumn('jy_farmer_plant_log',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'plant_id');
	$orderid=pdo_getcolumn('jy_farmer_plant',array('id'=>$plant_id,'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'除虫一次',
			'totype'=>1,
			'type'=>1,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('除虫完成','','/worker_plant//status=2');	
}
if($_RQ['op']=='worm'){
	$plant_id=pdo_getcolumn('jy_farmer_plant_log',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'plant_id');
	$orderid=pdo_getcolumn('jy_farmer_plant',array('id'=>$plant_id,'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'除草一次',
			'totype'=>2,
			'type'=>1,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('除草完成','','/worker_plant//status=2');	
}
if($_RQ['op']=='shifei'){
	$plant_id=pdo_getcolumn('jy_farmer_plant_log',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']),'plant_id');
	$orderid=pdo_getcolumn('jy_farmer_plant',array('id'=>$plant_id,'uniacid'=>$_SESSION['uniacid']),'order_id');
	$data=array(
			'order_id'=>$orderid,
			'pid'=>$_RQ['id'],
			'msg'=>'施肥一次',
			'totype'=>3,
			'type'=>1,
			'status'=>1,
	);
	$this->model('worker')->uplog($data,$worker['id']);
	exi('施肥完成','','/worker_plant//status=2');
}